package com.helloworld.demo.atomic;

import java.util.concurrent.atomic.AtomicReference;

public class ThreadAtomicReference {
    public static void main(String[] args) {
        User user1 = new User("张三",23);
        User user2 = new User("李四",24);
        User user3 = new User("王五",25);

        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(user1);

        atomicReference.compareAndSet(user1,user2);
        System.out.println(atomicReference);
        System.out.println(atomicReference.get());

        atomicReference.compareAndSet(user1,user3);
        System.out.println(atomicReference.get());

    }
}

class User{
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}